<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="openDialog()">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="roleList">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色" />
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.date ? '未启用' : '已启用' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column
          fixed="right"
          label="操作"
          width="190"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handlePrem(scope.row.id)">分配权限</el-button>
            <el-button type="text" size="small" @click="openDialog('preview', scope.row.id)">查看</el-button>
            <el-button type="text" size="small" @click="openDialog('edit', scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="page"
          :background="true"
          :page-size="pagesize"
          layout="prev, pager, next, sizes"
          :total="total"
          :page-sizes="[5, 6, 10, 8, 100]"
        />
      </el-row>
    </div>
    <el-dialog
      :title="title[type]"
      :visible.sync="isShow"
      width="30%"
    >
      <el-form ref="roleForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="角色名称:" prop="name">
          <el-input v-model="form.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用:" prop="state">
          <el-switch
            v-model="form.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述:" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <template v-if="isBtnShow">
                <el-button type="primary" size="mini" @click="submit">确定</el-button>
                <el-button size="mini" @click="closeDialog">取消</el-button>
              </template>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <assign-permission ref="refPerm" :show-dialog-assign.sync="showDialogAssign" :current-id="currentId" />
  </div>
</template>

<script>
import { editRole, getRole, getRoleDetail, newRole, removeRole } from '@/api/role'
import AssignPermission from './components/assignPermission.vue'
export default {
  name: 'Role',
  components: {
    AssignPermission
  },
  data() {
    return {
      currentId: -1,
      showDialogAssign: false,
      edid: 0,
      page: 1,
      pagesize: 10,
      roleList: [],
      total: 0,
      isShow: false,
      isBtnShow: true,
      type: 'add',
      title: {
        preview: '查看角色',
        edit: '编辑角色',
        add: '添加角色'
      },
      form: {
        name: '',
        state: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 6, message: '请输入正确的角色名', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 3, max: 100, message: '请输入正确的角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await getRole({ page: this.page, pagesize: this.pagesize })
      console.log(res)
      this.roleList = res.data.rows
      this.total = res.data.total
    },
    closeDialog() {
      this.isShow = false
      this.$refs.roleForm.resetFields()
    },
    async submit() {
      if (this.edid) {
        const editRes = await editRole({
          id: this.edid,
          ...this.form
        })
        console.log(editRes)
        this.$message.success('修改成功')
        this.closeDialog()
      } else {
        this.$refs.roleForm.validate(async valid => {
          if (valid) {
            const res = await newRole(this.form).catch(() => {})
            console.log(res)
            this.$message.success('添加成功')
            this.closeDialog()
          }
        })
      }
      // 更新表格数据
      // 想要看到刚才添加的数据,需要跳转到最后一页
      this.total = this.total + 1
      this.page = Math.ceil(this.total / this.pagesize)
      this.getRoleList()
    },
    async del(id) {
      this.$confirm('确认删除?').then(async() => {
        const res = await removeRole(id)
        console.log(res)
        if (this.roleList.length === 1) this.page -= 1
        this.getRoleList()
      }).catch(() => {})
    },
    async openDialog(type, id) {
      this.type = type
      this.isShow = true
      if (type === 'preview') {
        this.isBtnShow = false
        const res = await getRoleDetail(id)
        console.log(res)
        this.form = { ...res.data }
        this.getRoleList()
      }
      if (type === 'edit') {
        const getRes = await getRoleDetail(id)
        this.form = { ...getRes.data }
        this.edid = id
      }
    },
    handlePrem(id) {
      this.currentId = id
      this.showDialogAssign = true
      this.$nextTick(() => {
        this.$refs.refPerm.getPermiss()
      })
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
